<template>
  <el-card class="category" style="margin-bottom: 10px">
    <el-form :inline="true" :disabled="isDisabled" class="demo-form-inline">
      <el-form-item label="一级分类">
        <el-select v-model="category1Id" placeholder="请选择">
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="c1 in categoryStore.category1List"
            :key="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择">
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="c2 in categoryStore.category2List"
            :key="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="category3Id" placeholder="请选择">
          <el-option
            :label="c3.name"
            :value="c3.id"
            v-for="c3 in categoryStore.category3List"
            :key="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script lang="ts">
export default {
  // props: ['isDisabled'] API方法
  name: 'CtaegorySelector',
};
</script>
<script lang="ts" setup>
import { reqGetCategory1ListData } from '@/api/product/category';
import { reactive, onMounted, computed } from 'vue';
import { useCategoryListStore } from '@/stores/categoryList';

const categoryStore = useCategoryListStore();
const props = defineProps(['isDisabled']);

//1.页面挂载完毕后获取一级导航分类数据
onMounted(() => {
  categoryStore.getCategory1ListData();
});

// 2. 使用计算属性-一级分类id
const category1Id = computed<number>({
  get() {
    return categoryStore.category1Id as number;
  },
  set(val) {
    //获取二级分类数据
    categoryStore.getCategory2ListData(val);
  },
});

// 3. 使用计算属性-二级分类id
const category2Id = computed<number>({
  get() {
    return categoryStore.category2Id as number;
  },
  set(val) {
    // 根据当前二级分类去获取三级导航分类数据
    categoryStore.getCategory3ListData(val);
  },
});

// 4. 使用计算属性-三级分类id
const category3Id = computed<number>({
  get() {
    return categoryStore.category3Id as number;
  },
  set(val) {
    categoryStore.setCategory3Id(val);
  },
});

const onSubmit = () => {
  console.log('submit!');
};
</script>
<style scoped lang="scss">
.category {
  .el-card_body {
    .el-foem-item {
      margin-bottom: 0;
    }
  }
}
</style>
